//
//  LocationViewController.m
//  HuateVideo
//
//  Created by Tiger on 12-5-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LocationViewController.h"
#import "VideoPlayerViewController.h"
#import "FabuViewController.h"
#import "LocalVideo.h"
#import "UserLoginViewController.h"
#import "ShowOnePicViewController.h"
@interface LocationViewController ()

@end

@implementation LocationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)dealloc
{
    [table1 release];
    [table2 release];
    [array1 release];
    [array2 release];
    [uploadFile release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(loadVideoList) 
                                                 name: REFLASH_MY_VIDEOS_NOTIFICATION
                                               object: nil ];
    
    self.view.backgroundColor = TOOLBAR_TINTCOLOR;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:LS(@"拍摄") 
                                                              style:UIBarButtonItemStylePlain 
                                                             target:self 
                                                             action:@selector(startCamera)];
    self.navigationItem.rightBarButtonItem = right;
    [right release];
    
    [self initTopView];
    
    array1 = [[NSMutableArray alloc]init];
    array2 = [[NSMutableArray alloc]init];
    uploadFile = [[UploadFile alloc]init];
    uploadFile.delegate = self;
    currentUpload = -1;
    
    progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(95, 57, 215, 10)];
    progressView.progress = 0.0;
    if ([progressView respondsToSelector:@selector(setProgressTintColor:)]) {
        [progressView performSelector:@selector(setProgressTintColor:) withObject:[UIColor greenColor]];
    }
    
    progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, 35, 150, 20)];
    progressLabel.text = [NSString stringWithFormat:@"%@...",LS(@"上传中")];
    progressLabel.textColor = [UIColor lightGrayColor];
    progressLabel.font = [UIFont systemFontOfSize:14.0];
    progressLabel.backgroundColor = [UIColor clearColor];
    
    table1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 320, 327)];
    table1.tag = 101;
    table1.dataSource = self;
    table1.delegate = self;
    table1.allowsSelection = NO;
    table1.separatorStyle = UITableViewCellSeparatorStyleNone;
    table1.backgroundColor = [UIColor clearColor];
    [self.view addSubview:table1];
    table1.hidden = NO;
    
    table2 = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 320, 327)];
    table2.tag = 102;
    table2.dataSource = self;
    table2.delegate = self;
    table2.allowsSelection = NO;
    table2.separatorStyle = UITableViewCellSeparatorStyleNone;
    table2.backgroundColor = [UIColor clearColor];
    [self.view addSubview:table2];
    table2.hidden = YES;
    
    [self loadVideoList];
}

- (void)uploadingWithProgress:(CGFloat)progress
{
    progressView.progress = progress;
    progressLabel.text = [NSString stringWithFormat:@"%@：%0.1f%%",LS(@"上传中"),progressView.progress*100];
}

- (void)loadVideoList
{
    table2.userInteractionEnabled = YES;
    progressLabel.text = [NSString stringWithFormat:@"%@...",LS(@"上传中")];
    progressView.progress = 0.0;
    
    NSArray *myVideo = (NSArray *)[UserDefaults objectForKey:MY_VIDEOS_STORE_KEY];
    
    [array1 removeAllObjects];
    for (int i=0; i<[myVideo count]; i++) {
        LocalVideo *temp = [myVideo objectAtIndex:i];
        if ([self.title isEqualToString:LS(@"我的视频")] && [temp.fileType isEqualToString:@"V"]) {
            [array1 addObject:temp];
        }
        else if ([self.title isEqualToString:LS(@"我的图片")] && [temp.fileType isEqualToString:@"P"]) {
            [array1 addObject:temp];
        }
    }
    
    [array2 removeAllObjects];
    for (int i=0; i<[array1 count]; i++) {
        LocalVideo *localVideo = [array1 objectAtIndex:i];
        if (localVideo.ok == 1 || localVideo.ok == 3) {
            [array2 addObject:localVideo];
        }
    }
    
    [table1 reloadData];
    [table2 reloadData];
    
    
    if (!uploadFile.isUploading) {
        for (int i=0; i<[array2 count]; i++) {
            LocalVideo *lv = [array2 objectAtIndex:i];
            if (lv.ok != 3) {
                currentUpload = i;
                [uploadFile startUploadWithVideo:lv];
                break;
            }
        }
    }
}

- (void)initTopView
{
    NSString *name1 = LS(@"视频列表");
    
    if ([self.title isEqualToString:LS(@"我的图片")]) {
        name1 = LS(@"图片列表");
    }
    
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:name1,LS(@"上传列表"),nil]];
	segmentControl.frame = CGRectMake(10, 7, 140, 26);
    segmentControl.tintColor = TOOLBAR_TINTCOLOR;
	[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
	[segmentControl setSelectedSegmentIndex:0];
	[segmentControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
	[self.view addSubview:segmentControl];
	[segmentControl release];
    
    UIButton *bendi = [[UIButton alloc]initWithFrame:CGRectMake(225, 7, 85, 26)];
    [bendi setBackgroundImage:[UIImage imageNamed:@"shangchuan.png"] forState:UIControlStateNormal];
    [bendi setTitle:LS(@"从照片库添加") forState:UIControlStateNormal];
    [bendi addTarget:self action:@selector(startPhotoLibrary) forControlEvents:UIControlEventTouchUpInside];
    bendi.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self.view addSubview:bendi];
    [bendi release];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 39, 320, 2)];
    line.image = [UIImage imageNamed:@"fengexian.png"];
    [self.view addSubview:line];
    [line release];
}

- (void)segmentChanged:(id)sender
{
	if (((UISegmentedControl *)sender).selectedSegmentIndex == 0) {
		table1.hidden = NO;
        table2.hidden = YES;
	}
    else {
        table1.hidden = YES;
        table2.hidden = NO;
    }
}

- (void)startCamera
{
    NSArray *mediaTypes;
    if ([self.title isEqualToString:LS(@"我的视频")]) {
        mediaTypes= [NSArray arrayWithObject:@"public.movie"];
    }
    else if ([self.title isEqualToString:LS(@"我的图片")]) {
        mediaTypes = [NSArray arrayWithObject:@"public.image"];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:START_CAMERA_NOTIFICATION object: mediaTypes];
}

- (void)startPhotoLibrary
{
    NSArray *mediaTypes;
    if ([self.title isEqualToString:LS(@"我的视频")]) {
        mediaTypes= [NSArray arrayWithObject:@"public.movie"];
    }
    else if ([self.title isEqualToString:LS(@"我的图片")]) {
        mediaTypes = [NSArray arrayWithObject:@"public.image"];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:START_PHOTOLIBRARY_NOTIFICATION object: mediaTypes];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)playVideo
{
    //    Video *video = [[Video alloc]init];
    //    VideoPlayerViewController *videoPlayerViewController = [[VideoPlayerViewController alloc]initWithVideo:video];
    //    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:videoPlayerViewController];
    //    [self.navigationController presentModalViewController:nav animated:YES];
    //    [videoPlayerViewController release];
    //    [nav release];
    //    [video release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 101) {
        return [array1 count];
    }
    else {
        return [array2 count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	if (tableView.tag == 101) {
        static NSString* ListIdentifier = @"ListIdentifier1";
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifier];
        
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListIdentifier];
            //cell.selectionStyle = UITableViewCellSelectionStyleNone;  //去掉选中时的颜色
        }
        
        for (UIView* subView in [cell.contentView subviews])
        {
            [subView removeFromSuperview];
        }
        
        if (indexPath.row < [array1 count]) {
            LocalVideo *localVideo = [array1 objectAtIndex:indexPath.row];
            
            UIButton *image = [UIButton buttonWithType:UIButtonTypeCustom];
            image.frame = CGRectMake(10, 10, 75, 95);
            image.tag = 60000+indexPath.row;
            [image setBackgroundImage:[UIImage imageNamed:@"dengdai.png"] forState:UIControlStateNormal];
            [image addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:image];
            
            if (localVideo.image) {
                image.backgroundColor = [UIColor blackColor];
                [image setImage:localVideo.image forState:UIControlStateNormal];
            }
            
            if ([localVideo.fileType isEqualToString:@"V"]) {
                UIImageView *play = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
                play.image = [UIImage imageNamed:@"bofang.png"];
                play.center = CGPointMake(image.frame.size.width/2, image.frame.size.height/2);
                [image addSubview:play];
                [play release];
            }
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(95, 10, 200, 20)];
            label.text = localVideo.time;
            label.textColor = LIGHT_COLOR;
            label.font = [UIFont systemFontOfSize:16.0];
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];
            
            if (localVideo.title.length>0) {
                label.text = localVideo.title;
            }
            
            label = [[UILabel alloc]initWithFrame:CGRectMake(95, 35, 120, 20)];
            label.text = [NSString stringWithFormat:@"%@：%@",LS(@"大小"),[self getSizeString:localVideo.size]];
            label.textColor = [UIColor lightGrayColor];
            label.font = [UIFont systemFontOfSize:14.0];
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];
            
            if ([localVideo.fileType isEqualToString:@"V"]) {
                label = [[UILabel alloc]initWithFrame:CGRectMake(200, 35, 120, 20)];
                label.text = [NSString stringWithFormat:@"%@：%@",LS(@"时长"),[self getLongString:localVideo.seconds]];;
                label.textColor = [UIColor lightGrayColor];
                label.font = [UIFont systemFontOfSize:14.0];
                label.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:label];
                [label release];
            }
            
            if (localVideo.ok == 2) {
                
                label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 120, 20)];
                label.text = LS(@"已发布成功");
                label.textColor = [UIColor greenColor];
                label.font = [UIFont systemFontOfSize:16.0];
                label.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:label];
                [label release];
                
                UIButton *shanchu = [UIButton buttonWithType:UIButtonTypeCustom];
                shanchu.tag = 20000+indexPath.row;
                shanchu.frame = CGRectMake(255, 79, 55, 26);
                shanchu.titleLabel.font = [UIFont systemFontOfSize:12.0];
                [shanchu setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
                [shanchu setTitle:LS(@"删除") forState:UIControlStateNormal];
                [shanchu addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:shanchu];
            }
            else if (localVideo.ok == 1 || localVideo.ok == 3){
                label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 120, 20)];
                label.text = LS(@"正在发布...");
                label.textColor = [UIColor redColor];
                label.font = [UIFont systemFontOfSize:16.0];
                label.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:label];
                [label release];
                
                UIButton *quxiao = [UIButton buttonWithType:UIButtonTypeCustom];
                quxiao.tag = 30000+indexPath.row;
                quxiao.frame = CGRectMake(245, 79, 65, 26);
                quxiao.titleLabel.font = [UIFont systemFontOfSize:12.0];
                [quxiao setBackgroundImage:[UIImage imageNamed:@"sizianniu.png"] forState:UIControlStateNormal];
                [quxiao setTitle:LS(@"取消发布") forState:UIControlStateNormal];
                [quxiao addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:quxiao];
            }
            else if (localVideo.ok == 0) {
                UIButton *fabu = [UIButton buttonWithType:UIButtonTypeCustom];
                fabu.tag = 10000+indexPath.row;
                fabu.frame = CGRectMake(190, 79, 55, 26);
                fabu.titleLabel.font = [UIFont systemFontOfSize:12.0];
                [fabu setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
                [fabu setTitle:LS(@"发布") forState:UIControlStateNormal];
                [fabu addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:fabu];
                
                UIButton *shanchu = [UIButton buttonWithType:UIButtonTypeCustom];
                shanchu.tag = 20000+indexPath.row;
                shanchu.frame = CGRectMake(255, 79, 55, 26);
                shanchu.titleLabel.font = [UIFont systemFontOfSize:12.0];
                [shanchu setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
                [shanchu setTitle:LS(@"删除") forState:UIControlStateNormal];
                [shanchu addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:shanchu];
            }
            else if (localVideo.ok == 4) {
                label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 120, 20)];
                label.text = LS(@"发布失败");
                label.textColor = [UIColor grayColor];
                label.font = [UIFont systemFontOfSize:16.0];
                label.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:label];
                [label release];
                
                UIButton *shanchu = [UIButton buttonWithType:UIButtonTypeCustom];
                shanchu.tag = 20000+indexPath.row;
                shanchu.frame = CGRectMake(255, 79, 55, 26);
                shanchu.titleLabel.font = [UIFont systemFontOfSize:12.0];
                [shanchu setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
                [shanchu setTitle:LS(@"删除") forState:UIControlStateNormal];
                [shanchu addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:shanchu];
            }
            
            
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 114, 320, 2)];
            line.image = [UIImage imageNamed:@"fengexian.png"];
            [cell.contentView addSubview:line];
            [line release];
        }
        
        cell.backgroundColor = [UIColor clearColor];
        
        return cell;
    }
    else {
        static NSString* ListIdentifier = @"ListIdentifier2";
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifier];
        
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListIdentifier];
            //cell.selectionStyle = UITableViewCellSelectionStyleNone;  //去掉选中时的颜色
        }
        
        for (UIView* subView in [cell.contentView subviews])
        {
            [subView removeFromSuperview];
        }
        
        if (indexPath.row < [array2 count]) {
            LocalVideo *localVideo = [array2 objectAtIndex:indexPath.row];
            
            UIButton *image = [UIButton buttonWithType:UIButtonTypeCustom];
            image.frame = CGRectMake(10, 10, 75, 95);
            image.tag = 70000+indexPath.row;
            [image setBackgroundImage:[UIImage imageNamed:@"dengdai.png"] forState:UIControlStateNormal];
            [image addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:image];
            
            if (localVideo.image) {
                image.backgroundColor = [UIColor blackColor];
                [image setImage:localVideo.image forState:UIControlStateNormal];
            }
            
            if ([localVideo.fileType isEqualToString:@"V"]) {
                UIImageView *play = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
                play.image = [UIImage imageNamed:@"bofang.png"];
                play.center = CGPointMake(image.frame.size.width/2, image.frame.size.height/2);
                [image addSubview:play];
                [play release];
            }
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(95, 10, 200, 20)];
            label.text = localVideo.time;
            label.textColor = LIGHT_COLOR;
            label.font = [UIFont systemFontOfSize:16.0];
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];
            
            if (localVideo.title.length>0) {
                label.text = localVideo.title;
            }
            
            if (indexPath.row == currentUpload && localVideo.ok == 1) {
                
                [cell.contentView addSubview:progressLabel];
                
                [cell.contentView addSubview:progressView];
                
                UIButton *zanting = [UIButton buttonWithType:UIButtonTypeCustom];
                zanting.tag = 40000+indexPath.row;
                zanting.frame = CGRectMake(190, 79, 55, 26);
                zanting.titleLabel.font = [UIFont systemFontOfSize:12.0];
                [zanting setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
                [zanting setTitle:LS(@"暂停") forState:UIControlStateNormal];
                [zanting addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                [cell.contentView addSubview:zanting];
            }
            else {
                if (localVideo.ok == 1) {
                    label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 120, 20)];
                    label.text = LS(@"等待下载...");
                    label.textColor = [UIColor lightGrayColor];
                    label.font = [UIFont systemFontOfSize:16.0];
                    label.backgroundColor = [UIColor clearColor];
                    [cell.contentView addSubview:label];
                    [label release];
                }
                else if (localVideo.ok == 3) {
                    label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 120, 20)];
                    label.text = LS(@"已暂停...");
                    label.textColor = [UIColor lightGrayColor];
                    label.font = [UIFont systemFontOfSize:16.0];
                    label.backgroundColor = [UIColor clearColor];
                    [cell.contentView addSubview:label];
                    [label release];
                    
                    UIButton *zanting = [UIButton buttonWithType:UIButtonTypeCustom];
                    zanting.tag = 80000+indexPath.row;
                    zanting.frame = CGRectMake(190, 79, 55, 26);
                    zanting.titleLabel.font = [UIFont systemFontOfSize:12.0];
                    [zanting setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
                    [zanting setTitle:LS(@"继续") forState:UIControlStateNormal];
                    [zanting addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
                    [cell.contentView addSubview:zanting];
                }
            }
            
            UIButton *quxiao = [UIButton buttonWithType:UIButtonTypeCustom];
            quxiao.tag = 50000+indexPath.row;
            quxiao.frame = CGRectMake(255, 79, 55, 26);
            quxiao.titleLabel.font = [UIFont systemFontOfSize:12.0];
            [quxiao setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
            [quxiao setTitle:LS(@"取消") forState:UIControlStateNormal];
            [quxiao addTarget:self action:@selector(doEvent:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:quxiao];
            
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 114, 320, 2)];
            line.image = [UIImage imageNamed:@"fengexian.png"];
            [cell.contentView addSubview:line];
            [line release];
        }
        
        cell.backgroundColor = [UIColor clearColor];
        
        return cell;
    }
}

- (NSString *)getLongString:(long long)seconds
{
    NSString *second_str = @"00";
    NSString *minute_str = @"00";
    NSString *hour_str = @"00";
    
    NSInteger second = 0;
    NSInteger minute = 0;
    NSInteger hour = 0;
    
    second = seconds % 60;
    minute = ((int)floor((double)seconds / 60.0))%60;
    hour = (int)floor((double)seconds / 3600.0);
    
    second_str = [NSString stringWithFormat:@"%d",second];
    if (second < 10) {
        second_str = [NSString stringWithFormat:@"0%d",second];
    }
    
    minute_str = [NSString stringWithFormat:@"%d",minute];
    if (minute < 10) {
        minute_str = [NSString stringWithFormat:@"0%d",minute];
    }
    
    hour_str = [NSString stringWithFormat:@"%d",hour];
    if (hour < 10) {
        hour_str = [NSString stringWithFormat:@"0%d",hour];
    }
    
    return [NSString stringWithFormat:@"%@:%@:%@",hour_str,minute_str,second_str];
}

- (NSString *)getSizeString:(double)liuliang
{
    double ll = liuliang;
    NSString *rlt;
    NSArray *arrayDW = [NSArray arrayWithObjects:@"B",@"KB",@"MB",@"GB",@"TB", nil];
    NSInteger i=0;
    while (ll>=1024) {
        i++;
        ll = (double)ll/1024.0;
    }
    
    if (i<5) {
        rlt = [NSString stringWithFormat:@"%.1f%@",ll,[arrayDW objectAtIndex:i]];
    }
    else {
        rlt = @"未知";
    }
    return rlt;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        UserLoginViewController* login=[[UserLoginViewController alloc] init];
        UINavigationController* nav=[[UINavigationController alloc] initWithRootViewController:login];
        
        [self.navigationController presentModalViewController:nav animated:YES];
        [login release];
        [nav release];
    }
}

- (void)doEvent:(UIButton *)sender
{
    if (sender.tag >= 10000 && sender.tag < 20000) {
        NSString *user = [UserDefaults objectForKey:USERNAME_STORE_KEY];
        if (![user length]) {
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"您需要登陆才能进行发布操作")
                                                               delegate:self 	
                                                      cancelButtonTitle:LS(@"取消")
                                                      otherButtonTitles:LS(@"登陆"),nil];		
            [alertview show];		
            [alertview release];
            
        }
        else {
            NSInteger index = sender.tag - 10000;
            LocalVideo *localVideo = [array1 objectAtIndex:index];
            
            FabuViewController *fabuViewController = [[FabuViewController alloc]init];
            fabuViewController.localVideo = localVideo;
            [self.navigationController pushViewController:fabuViewController animated:YES];
            [fabuViewController release];
        }
    }
    else if (sender.tag >= 20000 && sender.tag < 30000) {
        NSInteger index = sender.tag - 20000;
        
        LocalVideo *localVideo = [array1 objectAtIndex:index];
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:localVideo.storePath]) {
            if ([[NSFileManager defaultManager] removeItemAtPath:localVideo.storePath error:nil]) {
                NSLog(@"删除视频文件成功");
            }
            else {
                NSLog(@"删除视频文件失败");
            }
        }
        else {
            NSLog(@"未找到视频文件");
        }
        
        if (index < [array1 count]) {
            [UserDefaults deleteLocalVideo:localVideo];
            [array1 removeObjectAtIndex:index];
        }
        if (array1) {
            
            [self loadVideoList];
        }
    }
    else if (sender.tag >= 30000 && sender.tag < 40000) {
        NSInteger index = sender.tag - 30000;
        LocalVideo *localVideo = [array1 objectAtIndex:index];
        localVideo.ok = 0;
        [UserDefaults restoreLocalVideo:localVideo];
        [self loadVideoList];
    }
    else if (sender.tag >= 40000 && sender.tag < 50000) {
        
        NSInteger index = sender.tag - 40000;
        LocalVideo *localVideo = [array2 objectAtIndex:index];
        localVideo.ok = 3;
        [UserDefaults restoreLocalVideo:localVideo];
        table2.userInteractionEnabled = NO;
        progressLabel.text = @"正在暂停...";
        
        [uploadFile wantToStop];
    }
    else if (sender.tag >= 50000 && sender.tag < 60000) {
        NSInteger index = sender.tag - 50000;
        LocalVideo *localVideo = [array2 objectAtIndex:index];
        localVideo.ok = 0;
        [UserDefaults restoreLocalVideo:localVideo];
        [self loadVideoList];
    }
    else if (sender.tag >= 60000 && sender.tag < 70000) {
        NSInteger index = sender.tag - 60000;
        LocalVideo *localVideo = [array1 objectAtIndex:index];
        
        if ([localVideo.fileType isEqualToString:@"P"]) {
            ShowOnePicViewController *showOnePicViewController = [[ShowOnePicViewController alloc]init];
            
            showOnePicViewController.title = localVideo.time;
            if (localVideo.title.length>0) {
                showOnePicViewController.title = localVideo.title;
            }
            
            [showOnePicViewController showPicture:localVideo.image];
            [self.navigationController pushViewController:showOnePicViewController animated:YES];
            [showOnePicViewController release];
        }
        else {
            if ([[NSFileManager defaultManager] fileExistsAtPath:localVideo.storePath]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:PLAYMOVIENOTIFICATION object:localVideo];
            }
        }
    }
    else if (sender.tag >= 70000 && sender.tag < 80000) {
        NSInteger index = sender.tag - 70000;
        LocalVideo *localVideo = [array2 objectAtIndex:index];
        
        if ([localVideo.fileType isEqualToString:@"P"]) {
            ShowOnePicViewController *showOnePicViewController = [[ShowOnePicViewController alloc]init];
            
            showOnePicViewController.title = localVideo.time;
            if (localVideo.title.length>0) {
                showOnePicViewController.title = localVideo.title;
            }
            
            [showOnePicViewController showPicture:localVideo.image];
            [self.navigationController pushViewController:showOnePicViewController animated:YES];
            [showOnePicViewController release];
        }
        else {
            if ([[NSFileManager defaultManager] fileExistsAtPath:localVideo.storePath]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:PLAYMOVIENOTIFICATION object:localVideo];
            }
        }
    }
    else if (sender.tag >= 80000 && sender.tag < 90000) {
        NSInteger index = sender.tag - 80000;
        LocalVideo *localVideo = [array2 objectAtIndex:index];
        
        if (localVideo.ok == 3) {
            localVideo.ok = 1;
            [UserDefaults restoreLocalVideo:localVideo];
            [self loadVideoList];
        }
        else {
            localVideo.ok = 1;
            [UserDefaults restoreLocalVideo:localVideo];
            [table2 reloadData];
        }
    }
}

@end
